package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.leaf.lowcode.domain.HIKGroupData;
import com.leaf.lowcode.domain.HIKSubData;
import com.leaf.lowcode.utils.HttpRequestUtil;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 发送设备运行时状态数据
 */
@Component
public class SendDevRunStatusDataService {


    //@Scheduled(initialDelay=1000, fixedRate=5000) 第一次延迟1秒后执行，之后按fixedRate的规则每5秒执行一次
   @Scheduled(initialDelay = 20 * 1000, fixedRate = 15 * 1000)
    public void execute(){
        //1.获取数据
        System.out.println("------currentTime:=="+System.currentTimeMillis());
        String url = "http://127.0.0.1:15002/hik/getCountGroup";
        HashMap<String, Object> map = new HashMap<>();
        map.put("regionId", "root000000");
        map.put("isCascade", "1");
        map.put("groupType", "0");
        map.put("statType", "0");
        map.put("pageNo", "1");
        map.put("pageSize", "50");
        String res = HttpRequestUtil.post(url, HttpRequestUtil.map2Json(map));
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject jsonObject1 = jsonObject.getJSONObject("msg");
        jsonObject1 = jsonObject1.getJSONObject("data");
        String json = jsonObject1.getJSONArray("list").toJSONString();
        List<HIKGroupData> hikDataList = JSON.parseObject(json,new TypeReference<List<HIKGroupData>>(){}); // Json 转List
        System.out.println(hikDataList.size());
        System.out.println(res);
        System.out.println("-------------------------");
        if(hikDataList.size()>0){
            for(HIKGroupData hok:hikDataList){
                String groupId = hok.getGroupId();
                url = "http://127.0.0.1:15002/hik/getRealTime";
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("ids", "\""+groupId+"\"");
                String res2 = HttpRequestUtil.post(url, HttpRequestUtil.map2Json(map2));
                jsonObject = JSONObject.parseObject(res2);
                jsonObject1 = jsonObject.getJSONObject("msg");
                json = jsonObject1.getJSONArray("data").toJSONString();
                List<HIKSubData> hikSubList = JSON.parseObject(json,new TypeReference<List<HIKSubData>>(){}); // Json 转List
                hok.setkIKSubDataList(hikSubList);
            }
        }
        //子设备列表
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if(subDeviceDTOList!=null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if ("1701001475".equalsIgnoreCase(pid)) {
                    System.out.println("捕捉到摄像头1701001475-----------------------");
                    JSONObject standardData = new JSONObject();
                    standardData.put("reportTs",System.currentTimeMillis());
                    JSONObject profile = new JSONObject();
                    profile.put("modelId",200418);
                    profile.put("poiCode","w0701012");
                    standardData.put("profile",profile);
                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                    for (HIKGroupData hikdata : hikDataList) {
                        if (sn.equalsIgnoreCase(hikdata.getGroupId())) {
//                            JSONObject properties= new JSONObject.fromObject(JSON.toJSONString();
//                            String properties =
                            standardData.put("properties",hikdata.getkIKSubDataList().get(0));
                            standardData.put("events", new JSONObject());
                            try {
                                String devRunStatus = standardData.toJSONString();
                                System.out.println("发送设备运行时状态数据==" + devRunStatus);
                                System.out.println("--人流------------------------------开始传输设备运行时状态数据");
                                wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunStatus, true);
                            } catch (Exception e) {
                                System.out.println("设备运行时状态数据数据传输出现异常");
                            }
                        }
                    }
                }
            }
        }else{
            System.out.println("没有获取到子设备列表");
        }

        //2.发送请求到http端
        //2.1 判断当前to'ken是否可用


        //2.2 不可用则重新请求

        //3.做对应的处理，如存入数据库，或者发送出去
    }
}
